package com.gingko.duominuo

import android.app.Application
import android.content.Context
import androidx.multidex.BuildConfig
import androidx.multidex.MultiDex
import com.facebook.stetho.Stetho
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.tencent.smtt.export.external.TbsCoreSettings
import com.tencent.smtt.sdk.QbSdk
import dagger.hilt.android.HiltAndroidApp
import java.util.*


@HiltAndroidApp
class App : Application() {

    override fun onCreate() {
        super.onCreate()
        MultiDex.install(this)
        setApp(this)
        initRefreshHeader()
        Stetho.initializeWithDefaults(this)
        initX5Kernel()
    }

    private fun initX5Kernel() {
        val map = mutableMapOf<String, Any>()
        map[TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER] = true
        map[TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE] = true
        QbSdk.initTbsSettings(map)
    }

    override fun attachBaseContext(base: Context) {
        val res = base.resources
        val config = res.configuration
        config.setLocale(Locale.getDefault())
        config.fontScale = 1.0f
        val context = base.createConfigurationContext(config)
        super.attachBaseContext(context)
    }

    /**
     * 设置全局的Header构建器
     */
    private fun initRefreshHeader() {
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout -> MaterialHeader(context) }
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout -> ClassicsFooter(context) }
        SmartRefreshLayout.setDefaultRefreshInitializer { context, layout -> layout.layout.tag = "close egg" }
    }


    companion object {
        lateinit var instance: Application
            @JvmStatic get
            private set

        @JvmStatic
        fun setApp(app: Application) {
            instance = app
        }
    }
}